package cn.xiaoliu.hrm.service.impl;

import cn.hutool.http.HttpUtil;
import cn.xiaoliu.hrm.domain.LoginUser;
import cn.xiaoliu.hrm.domain.UserMeal;
import cn.xiaoliu.hrm.dto.LoginDto;
import cn.xiaoliu.hrm.dto.RefreshDto;
import cn.xiaoliu.hrm.mapper.LoginUserMapper;
import cn.xiaoliu.hrm.mapper.UserMealMapper;
import cn.xiaoliu.hrm.prop.OauthDetails;
import cn.xiaoliu.hrm.prop.OauthProperties;
import cn.xiaoliu.hrm.service.ILoginUserService;
import cn.xiaoliu.hrm.util.AjaxResult;
import cn.xiaoliu.hrm.util.ValidUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoliu
 * @since 2021-12-16
 */
@Service
public class LoginUserServiceImpl extends ServiceImpl<LoginUserMapper, LoginUser> implements ILoginUserService {
    @Autowired
    private LoginUserMapper loginUserMapper;
    @Autowired
    private UserMealMapper userMealMapper;
    @Override
    public AjaxResult entering(LoginUser loginUser) {
        /**
         * 先保存auth服务中的t_login_user然后返回主键用来保存t_user_meal
         * 再将t_login_user的主键返回用来保存t_employee然后再将主键返回用来保存t_tenant
         */
        //保存t_login_user
        loginUserMapper.insert(loginUser);

        //保存t_user_meal
        UserMeal userMeal = new UserMeal();
        userMeal.setLoginId(loginUser.getId());
        userMeal.setMealId(loginUser.getMealId());
        userMeal.setExpireDate(new Date());
        userMealMapper.insert(userMeal);
        return AjaxResult.me().setSuccess(true).setMessage("可冲").setResultObj(loginUser.getId());
    }
    //用户注册
    @Override
    public AjaxResult register(LoginUser loginUser) {
        //保存login_user
        loginUserMapper.insert(loginUser);
        return AjaxResult.me().setResultObj(loginUser.getId());
    }

    /**
     * 用户登录认证
     * @param dto
     * @return
     */
    @Autowired
    private OauthProperties oauthProperties;

    @Override
    public AjaxResult login(LoginDto dto) {
        //参数校验使用jsr303校验
        //获取token
        String url = "http://localhost:4010/oauth/token?client_id=%s&client_secret=%s&grant_type=password&redirect_uri=http://www.baidu.com&username=%s&password=%s";
        //获取类型
        Integer type = dto.getType();
        /*String clientId ;
        String clientSecret;
        if (type == 0){//如果type为0就是后台登录
            clientId = "admin";
            clientSecret = "1";
        }else {//如果为1就是前台登录
            clientId = "website";
            clientSecret = "1";
        }*/
        OauthDetails oauthDetails = oauthProperties.getOauthDetails(type);
        ValidUtils.assertNotNull(oauthDetails,"类型不能为空" );
        //格式化替换url的参数
        url = String.format(url,oauthDetails.getClientId(),oauthDetails.getClientSecret(),dto.getUsername(),dto.getPassword());
        String data = HttpUtil.get(url);
        Map<String,Object> map = JSON.parseObject(data, Map.class);
        return AjaxResult.me().setResultObj(map);
    }

    /**
     * 获取最新的token
     * @param dto
     * @return
     */
    @Override
    public AjaxResult refresh(@Valid RefreshDto dto) {
        //参数校验使用jsr303校验
        //获取token
        String url = "http://localhost:4010/oauth/token?grant_type=refresh_token&refresh_token=%s&client_id=%s&client_secret=%s";
        //获取类型
        Integer type = dto.getType();
        OauthDetails oauthDetails = oauthProperties.getOauthDetails(type);
        ValidUtils.assertNotNull(oauthDetails,"类型不能为空" );
        //格式化替换url的参数
        url = String.format(url,dto.getRtoken(),oauthDetails.getClientId(),oauthDetails.getClientSecret());
        String data = HttpUtil.get(url);
        Map<String,Object> map = JSON.parseObject(data, Map.class);
        return AjaxResult.me().setResultObj(map);
    }
}
